home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* SpaceRemU *}
- {* Copyright (c) Julian M Bucknall 2001 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Algorithms Alfresco: Removing spaces with DFA *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- unit SpaceRemU;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Edit1: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- procedure Edit1Change(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- function aaRemoveSpaces1(const S : string) : string;
- var
- Inx : integer;
- State : (ScanningNormal, ScanningQuoted, ScanningSpaces);
- ResultLen : integer;
- Ch : char;
- begin
- if S = '' then begin
- Result := '';
- Exit;
- end;
- SetLength(Result, length(S));
- ResultLen := 0;
- State := ScanningNormal;
- for Inx := 1 to length(S) do begin
- Ch := S[Inx];
- case State of
- ScanningNormal :
- begin
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = ' ') then
- State := ScanningSpaces
- else if (Ch = '"') then
- State := ScanningQuoted;
- end;
- ScanningQuoted :
- begin
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = '"') then
- State := ScanningNormal;
- end;
- ScanningSpaces :
- begin
- if (Ch <> ' ') then begin
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = '"') then
- State := ScanningQuoted
- else
- State := ScanningNormal;
- end;
- end;
- end;
- end;
- if (State = ScanningQuoted) then begin
- Result := '';
- raise Exception.Create('Unbalanced quotes in input string');
- end
- else
- SetLength(Result, ResultLen);
- end;
-
- function aaRemoveSpaces2(const S : string) : string;
- var
- Inx : integer;
- State : (ScanningLeadSpaces, ScanningNormal,
- ScanningQuoted, ScanningSpaces);
- ResultLen : integer;
- Ch : char;
- begin
- if S = '' then begin
- Result := '';
- Exit;
- end;
- SetLength(Result, length(S));
- ResultLen := 0;
- State := ScanningLeadSpaces;
- for Inx := 1 to length(S) do begin
- Ch := S[Inx];
- case State of
- ScanningLeadSpaces :
- begin
- if (Ch <> ' ') then begin
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = '"') then
- State := ScanningQuoted
- else
- State := ScanningNormal;
- end;
- end;
- ScanningNormal :
- begin
- if (Ch = ' ') then
- State := ScanningSpaces
- else begin
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = '"') then
- State := ScanningQuoted;
- end;
- end;
- ScanningQuoted :
- begin
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = '"') then
- State := ScanningNormal;
- end;
- ScanningSpaces :
- begin
- if (Ch <> ' ') then begin
- inc(ResultLen);
- Result[ResultLen] := ' ';
- inc(ResultLen);
- Result[ResultLen] := Ch;
- if (Ch = '"') then
- State := ScanningQuoted
- else
- State := ScanningNormal;
- end;
- end;
- end;
- end;
- if (State = ScanningQuoted) then begin
- Result := '';
- raise Exception.Create('Unbalanced quotes in input string');
- end
- else
- SetLength(Result, ResultLen);
- end;
-
- procedure TForm1.Edit1Change(Sender: TObject);
- begin
- Label1.Caption := '';
- Label2.Caption := '';
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- Label1.Caption := '[' + aaRemoveSpaces1(Edit1.Text) + ']';
- Label2.Caption := '[' + aaRemoveSpaces2(Edit1.Text) + ']';
- end;
-
- end.
-